package com.macro.mall.tiny.modules.ums.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.ums.model.bo.CourseUserSelectBO;
import com.macro.mall.tiny.modules.ums.model.bo.UserCourseInstBO;
import com.macro.mall.tiny.modules.ums.service.UserCourseService;
import com.macro.mall.tiny.security.util.JwtTokenUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author: i_kun
 * @Desc: o_。
 * @create: 2025-06-26 14:49
 */
@Controller
@Tag(name = "CourseController",description = "课程管理模块")
@RequestMapping("/courseUser")
public class UserCourseController {

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private UserCourseService userCourseService;
    /**
     * 查询课程与用户信息
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
     public CommonResult selectCourseUser(CourseUserSelectBO courseUserSelectBO){
                if (courseUserSelectBO.getPageNum() ==null){
                    courseUserSelectBO.setPageNum(1);
                }
                if (courseUserSelectBO.getPageSize() ==null){
                    courseUserSelectBO.setPageSize(10);
                }
        Page page = userCourseService.selectPage(courseUserSelectBO);
        return CommonResult.success(CommonPage.restPage(page));
    }
    @RequestMapping(value = "/inst", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult inst(HttpServletRequest request , @Validated @RequestBody UserCourseInstBO userCourseInstBO){
        //获取当前登陆人的信息
        String authHeader = request.getHeader(tokenHeader);
        //登陆人的名称
        String username = jwtTokenUtil.getUserNameFromToken(authHeader.substring(this.tokenHead.length()));
        userCourseInstBO.setOperator(username);
        userCourseService.inst(userCourseInstBO);
        return CommonResult.success(null);
    }
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    @ResponseBody
        public CommonResult del(HttpServletRequest request ,@RequestParam("courseId") Long courseId,@RequestParam("userId") String userId){
        //获取当前登陆人的信息
        String authHeader = request.getHeader(tokenHeader);
        //登陆人的名称
        String username = jwtTokenUtil.getUserNameFromToken(authHeader.substring(this.tokenHead.length()));
        //TODO 学习过就不允许删除 增加校验
        userCourseService.del(courseId,userId,username);
        return CommonResult.success(null);
    }
}
